package com.dgut.crm.common.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.log4j.MDC;

import com.dgut.crm.system.entity.Mumber;

public class LogFilter implements Filter{

	private final static double DEFAULT_USERID= Math.random()*100000.0; 
    private Logger logger = Logger.getLogger(LogFilter.class);
	 @Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
			HttpServletRequest req=(HttpServletRequest)request;  
	        HttpSession session= req.getSession();  
	       /* if (session==null){  
	            MDC.put("userId",DEFAULT_USERID);    
	        }  
	        else{  
	            Mumber number=(Mumber)session.getAttribute("number");  
	            if (number==null){  
	                MDC.put("userId",DEFAULT_USERID);  
	                MDC.put("userName",DEFAULT_USERID);  
	            }  
	            else  
	            {  
	                MDC.put("userId",number.getId());  
	                MDC.put("userName",number.getUserName());  
	            }  
	        }  */
	  try {
		  System.out.println("拦截器运行。。。。");
		  logger.info("test yi bo");
		  chain.doFilter(request, response);  
	  }finally {
		MDC.clear();
	}
	        
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
